<?php
/*
 * 环境配置
 */
// 简写的 DIRECTORY_SEPARATOR
define('DS', DIRECTORY_SEPARATOR);

// 设置根目录
define( 'ROOT_DIR' , str_replace( DS , '/' , dirname(dirname(dirname( __FILE__ ) ) ) ) );

// 设置库目录
define( 'LIB_DIR' , ROOT_DIR . '/libs' );

// 上传目录
define('UPLOADS', ROOT_DIR . '/uploads');

// 模板目录
define('TEMPLATES', ROOT_DIR . '/templates');

// 定义指示 PHP4 或 PHP5 的常量
if (substr(PHP_VERSION, 0, 1) == '5') {
    define('PHP5', true);
    define('PHP4', false);
} else {
    define('PHP5', false);
    define('PHP4', true);
}

/*
 * 配置文件加载
 */
$database_conf = LIB_DIR . '/config/database.php';
if(is_readable($database_conf)) {
	include($database_conf);
}

/**
 * 载入指定类并返回实例
 *
 * @param string $className
 * @return object
 */
function import($className) {
    if (PHP5) {
        $classExists = class_exists($className, false);
    } else {
        $classExists = class_exists($className);
    }
    if (!$classExists) {
        if (!loadClass($className)) {
            return false;
        }
    }
    $obj = new $className();
    return $obj;
}

/**
 * 载入指定类的定义文件
 *
 * @param string $className
 * @return boolean
 */
function loadClass($className) {
    if (PHP5) {
        if (class_exists($className, false) || interface_exists($className, false)) { return true; }
    } else {
        if (class_exists($className)) { return true; }
    }

    if (preg_match('/[^a-z0-9\-_.]/i', $className) === 0) {
        $filename = getFilePath($className . '.php');
        if ($filename) {
            require($filename);
            if (PHP5) {
                if (class_exists($className, false) || interface_exists($className, false)) { return true; }
            } else {
                if (class_exists($className)) { return true; }
            }
        }
    }
    _error('类文件未找到',$filename);
    return false;
}

/**
 * 载入模块文件
 *
 * @param string $moduleName
 * @return object
 */
function loadModule($moduleName) {
	$className = 'modules_' . $moduleName;
	$filename = LIB_DIR . DS . 'modules' . DS . $moduleName . DS . 'mod_' . $moduleName . '.php';
    if (DIRECTORY_SEPARATOR == '/') {
        $filename = str_replace('\\', DS, $filename);
    } else {
        $filename = str_replace('/', DS, $filename);
    }
    if (is_file($filename)) {
    	include($filename);
    	$obj = new $className();
        return $obj;
    }
	_error('请求模块不存在',$filename);
	return false;
}

/**
 * 搜索文件
 *
 * @param string $filename
 * @return string/boolean
 */
function getFilePath($filename) {
    $filename = LIB_DIR . DS . str_replace('_', DS, $filename);
    if (DIRECTORY_SEPARATOR == '/') {
        $filename = str_replace('\\', DS, $filename);
    } else {
        $filename = str_replace('/', DS, $filename);
    }
    if (strtolower(substr($filename, -4)) != '.php') {
        $filename .= '.php';
    }
    if (is_file($filename)) { return $filename; }
    return false;
}

/**
 * 返回信息
 *
 * @param string $name
 * @return mixed
 */
function getInf($name) {
	if($name) {
	    if(isset($GLOBALS[MOBLOG][$name])) {
	    	return $GLOBALS[MOBLOG][$name];
	    }
	}
	return false;
}

/**
 * 输出变量的内容，通常用于调试
 *
 * @param mixed $vars 要输出的变量
 * @param string $label
 * @param boolean $return
 */
function dump($vars, $label = '', $return = false)
{
    if (ini_get('html_errors')) {
        $content = "<pre>\n";
        if ($label != '') {
            $content .= "<strong>{$label} :</strong>\n";
        }
        $content .= htmlspecialchars(print_r($vars, true));
        $content .= "\n</pre>\n";
    } else {
        $content = $label . " :\n" . print_r($vars, true);
    }
    if ($return) { return $content; }
    echo $content;
    return null;
}

function _error($title=null, $error=null, $msg=null) {
	$title = $title?$title:'';
	$error = $error?$error:'';
	$msg = $msg?$msg:'';
	include LIB_DIR . DS . '_error.php';
}